/**
 * 
 */
package com.xbang.attendance.web.action.system.product;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.xbang.attendance.entity.product.Product;
import com.xbang.attendance.service.ProductService;
import com.demo2do.lighturl.annotation.Action;
import com.xbang.core.page.Page;
import com.xbang.core.page.Result;
import com.xbang.core.struts.support.SearchActionSupport;

/**
 * @author dev.zhang
 * 
 */
public class Index extends SearchActionSupport {

	private static final long serialVersionUID = 6137752167041062321L;

	private static final Log logger = LogFactory.getLog(Index.class);

	@Autowired
	private ProductService productService;

	private List<Product> products = new ArrayList<Product>();

	@Action("/system/product/${currPageId}/index/")
	public String listByPage() throws Exception {
	//	System.out.println(currPageId);
		return list();
	}
	/*
	 * (non-Javadoc)
	 * 
	 * @see com.opensymphony.xwork2.ActionSupport#execute()
	 */
	@Override
	public String execute() throws Exception {

		logger.info(">>>>>>>>> product");

		String hqlSentence = "FROM Product product WHERE 1= 1";

		if (page == null) {
			page = new Page(10);
		}
		Result result = productService.searchByPage(hqlSentence, searchComponent, page);

		products = result.getItems();
		page = result.getPage();
		return super.execute();
	}
	
	@Action("/system/product/list")
	public String list() throws Exception {

		String hqlSentence = "FROM Product product WHERE 1 = 1 ";
		if (searchComponent.getOrderParameters().isEmpty()){
			searchComponent.addSearchElement("orderBy.product.productCode", "ASC");
		}
		
		if (page != null) {
			if (currPageId.intValue() != -1 && currPageId.intValue() > 0) {
				page.setCurrentPage(currPageId);
			}
		}
		Result result = productService.searchByPage(hqlSentence, searchComponent, page);
		products = result.getItems();
		page = result.getPage();

		return "index";
	}

	/**
	 * @return the products
	 */
	public List<Product> getProducts() {
		return products;
	}

	/**
	 * @param products
	 *            the products to set
	 */
	public void setProducts(List<Product> products) {
		this.products = products;
	}

	/**
	 * @param productService
	 *            the productService to set
	 */
	public void setProductService(ProductService productService) {
		this.productService = productService;
	}

	@Override
	public void resetCustomerSearchCriteria() {

	}

}
